#include "../base.h"
#include "../include/TextView.h"
#include <stdio.h>

#define LOG_TAG "TextView"

extern jobject getActivity();

static jclass getClass(JNIEnv* env,const char * className)
{
	return (*env)->FindClass(env,className);
}

static jclass getTextViewClass(JNIEnv* env)
{
	return getClass(env,"android/widget/TextView");
}

//新建一个文本视图
TextView newTextView()
{
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getTextViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID( env,clazz,"<init>","(Landroid/content/Context;)V" );
	jobject obj = (*env)->NewObject(env,clazz,methoID,getActivity());
	TextView view = (*env)->NewGlobalRef(env,obj);
	(*env)->DeleteLocalRef(env,obj);
	(*env)->DeleteLocalRef(env,clazz);
	return view;
}

//删除一个文本视图
void deleteTextView(TextView thiz)
{
	if(thiz==NULL)
	{
		LOGE("deleteTextView thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	(*env)->DeleteGlobalRef(env,thiz);
}

//设置文本
void TextView_setText(TextView thiz,jstring text)
{
	if(thiz==NULL)
	{
		LOGE("TextView_setText thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getTextViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"setText","(Ljava/lang/CharSequence;)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,text);
}

//设置文本
void TextView_setTextEx(TextView thiz,const char *text)
{
	jstring str = createJString(text);
	TextView_setText(thiz,str);
	deleteJString(str);
}

static jstring toString(jobject obj)
{
	JNIEnv* env = Android_JNI_GetEnv();
	jclass  charseqClass = getClass(env,"java/lang/Object");
	static jmethodID charseqClassMethoID = NULL;
	if(charseqClassMethoID==NULL)
		charseqClassMethoID = (*env)->GetMethodID(env,charseqClass,"toString","()Ljava/lang/String;");
	jstring localStr = (*env)->CallObjectMethod(env,obj,charseqClassMethoID);
	(*env)->DeleteLocalRef(env,charseqClass);
	jstring str = (*env)->NewGlobalRef(env,localStr);
	(*env)->DeleteLocalRef(env,localStr);
	return str;
}

//得到文本内容，需要通过deleteJString释放
jstring TextView_getText(TextView thiz)
{
	if(thiz==NULL)
	{
		LOGE("TextView_getText thiz==NULL");
		return NULL;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getTextViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"getText","()Ljava/lang/CharSequence;");
	(*env)->DeleteLocalRef(env,clazz);
	jobject obj = (*env)->CallObjectMethod(env,thiz,methoID);
	jstring str = toString(obj);
	(*env)->DeleteLocalRef(env,obj);
	return str;
}

//得到文本内容，需要通过free释放
char *  TextView_getTextEx(TextView thiz)
{
	jstring str = TextView_getText(thiz);
	char *text = getJStringChars(str);
	deleteJString(str);
	return text;
}

//设置文字大小
void    TextView_setTextSize(TextView thiz,float size)
{
	if(thiz==NULL)
	{
		LOGE("TextView_setTextSize thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getTextViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"setTextSize","(F)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,size);
}

//设置文字颜色
void    TextView_setTextColor(TextView thiz,int color)
{
	if(thiz==NULL)
	{
		LOGE("TextView_setTextColor thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getTextViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"setTextColor","(I)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,color);
}

//设置文本是否为可选择的，默认为false
void	TextView_setTextIsSelectable(TextView thiz,bool selectable)
{
	if(thiz==NULL)
	{
		LOGE("TextView_setTextIsSelectable thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getTextViewClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"setTextIsSelectable","(Z)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,selectable);
}

